package gr.handmademarket.bl.operations;

import gr.handmademarket.hibernate.HibernateUtil;
import gr.handmademarket.po.implementations.Buyer;
import gr.handmademarket.po.implementations.Seller;
import gr.handmademarket.po.interfaces.User;

import org.hibernate.Session;

/**
 * login operation.
 */
public class LoginOperation {

	/**
	 * the user.
	 */
	private User user;
	
	/**
	 * username.
	 */
	private String username;
	
	/**
	 * password.
	 */
	private String password;
	
	/**
	 * Creates a new RegisterBuyerOperation object. 
	 *
	 */
	public LoginOperation() {
		// empty
	}
	
	/**
	 * execution method.
	 */
	public void execute(){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        if (session.get(Buyer.class, username) != null){
        	Buyer buyer = (Buyer) session.get(Buyer.class, username);
        	if (buyer.getPassword().equals(password)){
        		user = buyer;
        	}
        } else if (session.get(Seller.class, username) != null) {
        	Seller seller = (Seller) session.get(Seller.class, username);
        	if (seller.getPassword().equals(password)){
        		user = seller;
        	}
        }
	}

	/**
	 * @return user.
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param username
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @param password
	 */
	public void setPassword(String password) {
		this.password = password;
	}	
	
}
